# -*- coding: utf-8 -*-

# 模拟时钟

import sys, pygame, random, math
from pygame.locals import *
import time as t
from datetime import datetime, date, time

reload(sys)
sys.setdefaultencoding('utf-8')


def print_text(font, x, y, text, color=(255,255,255), shadow=False):
    if shadow:
        imgText = font.render(text, True, (0, 0, 0))
        screen.blit(imgText, (x - 2, y - 2))
    image = font.render(text, True, color)
    screen.blit(image, (x, y))


def wrap_angle(angle):
    return abs(angle %360)

pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("模拟时钟")
font1 = pygame.font.Font("freesansbold.ttf", 20)

white = 255, 255, 255
pink = 255, 100, 100
orange = 220, 180, 0
yellow = 255, 255, 0
pos_x = 300
pos_y = 250
radius = 200
angle = 360

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()

    screen.fill((0, 0, 100))

    # 表盘
    pygame.draw.circle(screen, white, (pos_x, pos_y), radius, 6)

    for n in range(1, 13):
        angle = math.radians(n*(360/12)-90)
        x = math.cos(angle) * (radius-20)-10
        y = math.sin(angle) * (radius-20)-10
        print_text(font1, pos_x+x,pos_y+y, str(n))

    # 指针
    today = datetime.today()
    hours = today.hour % 12
    hour_angle = wrap_angle(hours*(360/12)-90)
    hour_angle = math.radians(hour_angle)
    hour_x = math.cos(hour_angle) * (radius-100)
    hour_y = math.sin(hour_angle) * (radius-100)
    target = (pos_x+hour_x, pos_y+hour_y)

    pygame.draw.line(screen, pink, (pos_x,pos_y), target, 25)


    minutes = today.minute
    minutes_angle = wrap_angle(minutes * (360 / 60) - 90)
    minutes_angle = math.radians(minutes_angle)
    minutes_x = math.cos(minutes_angle) * (radius - 60)
    minutes_y = math.sin(minutes_angle) * (radius - 60)
    target = (pos_x + minutes_x, pos_y + minutes_y)

    pygame.draw.line(screen, orange, (pos_x, pos_y), target, 12)

    seconds = today.second
    seconds_angle = wrap_angle(seconds * (360 / 60) - 90)
    seconds_angle = math.radians(seconds_angle)
    seconds_x = math.cos(seconds_angle) * (radius - 40)
    seconds_y = math.sin(seconds_angle) * (radius - 40)
    target = (pos_x + seconds_x, pos_y + seconds_y)

    pygame.draw.line(screen, yellow, (pos_x, pos_y), target, 6)

    pygame.draw.circle(screen, white, (pos_x, pos_y), 20)

    print_text(font1, 0, 0, str(hours)+":"+str(minutes)+":"+str(seconds))

    pygame.display.update()

    t.sleep(0.016)

